package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConst;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealDao setMealDao;

    @Override
    @Transactional
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        log.debug("SetMealService: add:　setmeal　===> "+setmeal);
        log.debug("SetMealService: add:　checkGroupIds　===> "+checkGroupIds);
        setMealDao.add(setmeal);
        log.debug("主键回显: setmeal.getId　===> "+setmeal.getId());
        if (setmeal.getId()>0 && checkGroupIds!=null && checkGroupIds.length>0){
            for (Integer checkGroupId : checkGroupIds) {
                setMealDao.addRelation(setmeal.getId(),checkGroupId);
            }
        }
        jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES,setmeal.getImg());
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("SetMealService: findByPage:　queryPageBean　===> "+queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setMealDao.findPage(queryPageBean.getQueryString());
        log.debug("SetMealService: findByPage:　page　===> "+page.toString());
        return new PageResult(page.getTotal(),page);
    }

    @Override
    @Transactional
    public void delById(Integer id) {
        log.debug("SetMealService: delById:　id　===> "+id);
        setMealDao.delRelationById(id);
        setMealDao.delById(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        log.debug("SetMealService: findById:　id　===> "+id);
        Setmeal setmeal = setMealDao.findById(id);
        log.debug("SetMealService: findById:　setmeal　===> "+setmeal);
        return setmeal;
    }

    @Override
    public List<Integer> findCheckGroupIdsById(Integer id) {
        log.debug("SetMealService: findCheckGroupIdsById:　id　===> "+id);
        List<Integer> checkGroupIdsList = setMealDao.findCheckGroupIdsById(id);
        log.debug("SetMealService: findCheckGroupIdsById:　checkGroupIdsList　===> "+checkGroupIdsList);
        return checkGroupIdsList;
    }

    @Override
    @Transactional
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        log.debug("SetMealService: edit:　setmeal　===> "+setmeal);
        log.debug("SetMealService: edit:　checkGroupIds　===> "+checkGroupIds);
        setMealDao.edit(setmeal);
        setMealDao.delRelationById(setmeal.getId());
        if (setmeal.getId()!=null && checkGroupIds!=null && checkGroupIds.length>0){
            for (Integer checkGroupId : checkGroupIds) {
                setMealDao.addRelation(setmeal.getId(),checkGroupId);
            }
        }
        jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES,setmeal.getImg());
    }

    @Override
    public List<Setmeal> findAllSetMeal() {
        log.debug("SetMealService: findAllSetMeal: ===> ");
        List<Setmeal> setMealList = setMealDao.findAll();
        log.debug("SetMealService: findAllSetMeal:　setMealList　===> " + setMealList);
        return setMealList;
    }

    @Override
    public Setmeal findSetMealById(Integer id) {
        log.debug("SetMealService: findSetMealById: ===> id" + id);
        Setmeal setmeal = setMealDao.findSetMealById(id);
        log.debug("SetMealService: findSetMealById: ===> setmeal" + setmeal);
        return setmeal;
    }

    @Override
    public List<Map<String, Object>> findSetMealCount() {
        List<Map<String,Object>> mapList = setMealDao.findSetMealCount();
        log.debug("SetMealService: findSetMealCount: ===> mapList" + mapList);
        return mapList;
    }
}
